package com.piece.mall.controller;

import com.piece.core.web.controller.FrameWorkController;
import com.piece.mall.api.dto.CartDTO;
import com.piece.mall.api.dto.CartItemDTO;
import com.piece.mall.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

@Slf4j
@Controller
@Api(tags = {"购物车管理"})
@RequestMapping("/public/mall/cart")
public class CartController extends FrameWorkController {

    @Resource
    private CartService cartService;

    @GetMapping(value = "/item/add")
    @ApiOperation(value = "添加商品到购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name="skuId", value = "商品id"),
            @ApiImplicitParam(name="num", value = "商品数量"),
    })
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num) throws ExecutionException, InterruptedException {
        // 添加sku商品到购物车
        cartService.addToCart(skuId, num);
        getRequest().setAttribute("skuId", skuId);
        // 请求重定向添加成功页面，防刷
        return "redirect:public/mall/cart/success.html";
    }

    @GetMapping(value = "/success.html")
    @ApiOperation(value = "商品添加购物车成功页")
    @ApiImplicitParam(name="skuId", value = "商品id")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId) {
        //重定向到成功页面。再次查询购物车数据即可
        CartItemDTO item = cartService.getCartItem(skuId);
        getRequest().setAttribute("cartItem", item);
        return "/mall/cart/success";
    }

    @GetMapping("/list.html")
    @ApiOperation(value = "购物车列表页")
    public String cartListPage() throws ExecutionException, InterruptedException {
        CartDTO cart = cartService.getCart();
        getRequest().setAttribute("cart", cart);
        return "/mall/cart/list";
    }

    @GetMapping(value = "/item/check")
    @ApiOperation(value = "更改购物车商品选中状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name="skuId", value = "商品id"),
            @ApiImplicitParam(name="checked", value = "是否选中"),
    })
    public String checkItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "checked") Integer check) {
        cartService.changeItemChecked(skuId, check);
        return "redirect:public/mall/cart/list.html";
    }

    @GetMapping(value = "/item/count")
    @ApiOperation(value = "更改购物车商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name="skuId", value = "商品id"),
            @ApiImplicitParam(name="num", value = "商品数量"),
    })
    public String countItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "num") Integer num) {
        cartService.changeItemCount(skuId,num);
        return "redirect:public/mall/cart/list.html";
    }

    @GetMapping(value = "/item/delete")
    @ApiOperation(value = "删除购物车商品")
    @ApiImplicitParam(name="skuId", value = "商品id")
    public String deleteItem(@RequestParam("skuId") Integer skuId) {
        cartService.deleteCartItem(skuId);
        return "redirect:public/mall/cart/list.html";
    }
}
